package managedbean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import dao.ClientDao;

import model.Client;


@ManagedBean(name="registrationManagedBean")
@SessionScoped
public class RegistrationManagedBean {
	private Client user;
	private String password;
	private String identifiant;
	private String confirm;
	public ClientDao getClientDao() {
		return clientDao;
	}
	public void setClientDao(ClientDao clientDao) {
		this.clientDao = clientDao;
	}
	public Client getUser() {
		return user;
	}
	public void setUser(Client user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getIdentifiant() {
		return identifiant;
	}
	public void setIdentifiant(String identifiant) {
		this.identifiant = identifiant;
	}
	public String getConfirm() {
		return confirm;
	}
	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}
	public boolean isConnected() {
		return connected;
	}
	public void setConnected(boolean connected) {
		this.connected = connected;
	}
	private boolean connected;
	
	public static Logger log =Logger.getLogger(RegistrationManagedBean.class);
	@ManagedProperty(value="#{clientDao}")
	private ClientDao clientDao;

	
	public String save(){
		if(getPassword().equals(getConfirm())){
			if(getClientDao().exists(getIdentifiant())){
				FacesMessage messLogin= new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"confirm", 
						"Identifiant déjà utilisé, choisissez en un nouveau.");
				FacesContext.getCurrentInstance().addMessage("inscriptions", messLogin);
				return "fail";
			}
			boolean valid=getClientDao().save(getIdentifiant(), getPassword());
			if(valid==true){
			return "register";
			}
		}
		else{
			FacesMessage mess= new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"confirm", 
					"Les deux mots de passes sont différents");
			FacesContext.getCurrentInstance().addMessage("inscriptions", mess);
		}
		return "fail";
	}
}
